<?php

class HolidaysController extends AppController {
	
	public $uses = array('Holiday', 'User');
	
	public function beforeFilter() {
		parent::beforeFilter();
    	
	}
	
	
	public $paginate = array(
        'limit' => 20,
        'order' => array(
            'Holiday.start_date' => 'asc')
    );

    public function index() {
		if ($this->Session->read('Auth.User.role') === 'superadmin' || $this->Session->read('Auth.User.role') === 'admin') { 
			$this->Holiday->recursive = -1;
			$this->set('holidays', $this->paginate()); 
			$this->set('users', $this->User->find('all'));
		}
		else{ $this->redirect(array('controller' => 'timesheets', 'action' => 'index')); }  
    }

    public function view($id = null) {
		if ($this->Session->read('Auth.User.role') === 'superadmin' || $this->Session->read('Auth.User.role') === 'admin') { 
			$this->User->id = $id;
			if (!$this->User->exists()) {
				throw new NotFoundException(__('Invalid user'));
			}
			$this->set('user', $this->User->read(null, $id));
			$this->set('departments', $this->Department->find('all'));//gat the department from the departments table
		
		}else{ $this->redirect(array('controller' => 'timesheets', 'action' => 'index')); }
    }

    public function add() {
		if ($this->Session->read('Auth.User.role') === 'superadmin' || $this->Session->read('Auth.User.role') === 'admin') {
			if ($this->request->is('post')) {
				$this->Holiday->create();
				$this->request->data['Holiday']['created_by'] = $this->Auth->user('id'); //Added this line
				if ($this->Holiday->save($this->request->data)) {
					$this->Session->setFlash(__('The holiday has been added'));
					$this->redirect(array('action' => 'index'));
				} else {
					$this->Session->setFlash(__('The holiday could not be saved. Please, try again.'));
				}
			}
		}
		else{ $this->redirect(array('controller' => 'timesheets', 'action' => 'index')); }     
    }

    public function edit($id = null) {
		if (!$id) {
			throw new NotFoundException(__('Invalid holiday'));
		}
	
		$holiday = $this->Holiday->findById($id);
		if (!$holiday) {
			throw new NotFoundException(__('Invalid holiday'));
		}
	
		if ($this->request->is('holiday') || $this->request->is('put')) {
			$this->Holiday->id = $id;
			$this->request->data['Holiday']['modified_by'] = $this->Auth->user('id'); //Added this line
			if ($this->Holiday->save($this->request->data)) {
				$this->Session->setFlash('Your holiday has been updated.');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash('Unable to update your holiday.');
			}
		}
	
		if (!$this->request->data) {
			$this->request->data = $holiday;
		}
	}
	

    public function delete($id = null) {
		if ($this->Session->read('Auth.User.role') === 'superadmin' || $this->Session->read('Auth.User.role') === 'admin') {
			if (!$this->request->is('post')) {
				throw new MethodNotAllowedException();
			}
			$this->Holiday->id = $id;
			if (!$this->Holiday->exists()) {
				throw new NotFoundException(__('Invalid holiday'));
			}
			if ($this->Holiday->delete()) {
				$this->Session->setFlash(__('Holiday deleted'));
				$this->redirect(array('action' => 'index'));
			}
			$this->Session->setFlash(__('Holiday was not deleted'));
			$this->redirect(array('action' => 'index'));
		}
		else{ $this->redirect(array('controller' => 'timesheets', 'action' => 'index')); }
		
        
    }
	
	public function changepassword($id = null){
		$this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The password has been saved'));
                $this->redirect(array('controller'=>'timesheets','action' => 'index'));
            } else {
                $this->Session->setFlash(__('The password could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->User->read(null, $id);
            unset($this->request->data['User']['password']);
        }
	}
}

?>
